#include "keywordtreewidget.h"
#include <QChildEvent>
#include <QDebug>

KeywordTreeWidget::KeywordTreeWidget(QWidget *parent) :
    QTreeWidget(parent), m_lastRemoved(NULL)
{
}

void KeywordTreeWidget::childEvent(QChildEvent * ev)
{
    if(ev->removed()) {
        QObject * child = ev->child();
        ev->child()->dumpObjectInfo();
        m_lastRemoved = dynamic_cast<QTreeWidgetItem*>(ev->child());
        if(m_lastRemoved) {
            m_lastRemovedParent = m_lastRemoved->parent();
        }
        else {
            m_lastRemovedParent = NULL;
        }
    }
    else if(ev->added() && (ev->child() == (void*)m_lastRemoved) && (m_lastRemovedParent != NULL)) {
        QTreeWidgetItem * parent = m_lastRemoved->parent();
        QString source = "TREE ROOT";
        if(m_lastRemovedParent)
            source = m_lastRemovedParent->text(0);
        QString target = "TREE ROOT";
        if(parent)
            target = parent->text(0);
        qDebug() << "moved " << m_lastRemoved->text(0) << " from " << source << " to " << target;
    }
}
